<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Signup extends UserOAuth_Controller{
	
	// 管理员
	private $isadmin;

	public function __construct(){
		parent::__construct();
		// $this->show_message('系统维护中，大约10:30前恢复');
		$this->isadmin = $this->input->get('isadmin');
		if(!$this->isadmin){
			$this->islogin();
		}
		$this->load->model('signup_record_model');
	}
	
	public function index(){
		$this->load->helper('array');
		$this->load->model(['school_model', 'grade_model', 'campus_model']);
		$school_list = $this->school_model->get_result([
			'select' => ['id', 'title'],
		]);
		
		$res_grade = array_column2($this->grade_model->get_result(), 'school_id');
		$res_campus = array_column2($this->campus_model->get_result(), 'school_id');
		
		foreach($school_list as &$row){
			$schid = $row['id'];
			
			// 校区
			$campus_list = array();
			foreach($res_campus[$schid] as $value){
				$id = $value['id'];
				$campus_list[$id] = [
					'id' => $id,
					'title' => $value['title'],
					'address' => $value['address']
				];
			}
			$row['campus_list'] = $campus_list;
			
			// 年级
			$grade_list = array();
			foreach($res_grade[$schid] as $value){
				$campus_arr = array();
				$campusid_arr = explode(',', $value['campusids']);
				foreach($campusid_arr as $cid){
					$campus_arr[] = [
						'campus_id' => $cid,
						'plan_num' => 0,
						'sign_num' => 0,
						'show_sign_num' => 0
					];
				}
				$grade_list[] = [
					'id' => $value['id'],
					'title' => $value['title'],
					'start_time' => '即刻起',
					'end_time' => '2020年9月30日',
					'campus' => $campus_arr
				];
			}
			$row['grade_list'] = $grade_list;
		}
		$assign = [
			'user' => $this->_user,
			'school_json' => json_encode($school_list),
			'options' => [
				'follow_way' => $this->signup_record_model->dict_options('follow_way')
			]
		];
		$this->ci_smarty->assign($assign);
		$this->ci_smarty->display('signup.htm');
	}
	
	// 报名记录
	public function record(){
		$list = $this->signup_record_model->get_result([
			'where' => [
				'user_id' => $this->uid
			]
		]);
		$status_arr = $this->signup_record_model->dict_options('status');
		foreach($list as &$row){
			$row['status_text'] = '报名成功, '.$status_arr[$row['status']];
		}
		$assign = [
			'options' => [
				'status' => $status_arr
			],
			'list' => $list
		];
		$this->ci_smarty->assign($assign);
		$this->ci_smarty->display('signup_record.htm');
	}
	
	// 详情
	public function detail($id = ''){
		$where = [
			'id' => $id
		];
		if(!$this->isadmin){
			$where['user_id'] = $this->uid;
		}
		$detail = $this->signup_record_model->get_row([
			'where' => $where
		]);
		if(!$detail){
			$this->show_message('页面不存在');
		}
		$options = $this->signup_record_model->dict_options();
		$detail['status_text'] = '报名成功, '.$options['status'][$detail['status']];
		$assign = [
			'options' => $options,
			'detail' => $detail
		];
		$this->ci_smarty->assign($assign);
		$this->ci_smarty->display('signup_detail.htm');
	}
	
	// 报名结果
	public function result(){
		$this->ci_smarty->display('signup_result.htm');
	}
	
	// 报名提交
	public function submit(){
		$posts = $this->input->post();
		if(!$posts){
			$this->output_json();
		}
		$this->load->library('form_validation');
		$this->form_validation->set_rules('school', '申请就读学校', 'required');
		$this->form_validation->set_rules('school_id', '申请就读学校', 'required');
		$this->form_validation->set_rules('grade', '申请就读年级', 'required');
		$this->form_validation->set_rules('grade_id', '申请就读年级', 'required');
		$this->form_validation->set_rules('campus', '申请就读校区', 'required');
		$this->form_validation->set_rules('campus_id', '申请就读校区', 'required');
		$this->form_validation->set_rules('name', '姓名', 'required');
		$this->form_validation->set_rules('idno', '身份证号', 'required|valid_idcard');
		$this->form_validation->set_rules('nation', '民族', 'required');
		$this->form_validation->set_rules('height', '身高', 'required');
		$this->form_validation->set_rules('weight', '体重', 'required');
		$this->form_validation->set_rules('live_address', '现居住地址', 'required');
		$this->form_validation->set_rules('family_address', '家庭详细地址', 'required');
		$this->form_validation->set_rules('now_school', '现就读学校', 'required');
		$this->form_validation->set_rules('now_grade', '现就读年级', 'required');
		$this->form_validation->set_rules('appraise', '自我评价', 'required');
		$this->form_validation->set_rules('parent_name', '联系人姓名', 'required');
		$this->form_validation->set_rules('parent_phone', '联系人手机号码', 'required|valid_phone');
		$this->form_validation->set_rules('parent_clan', '与学生关系', 'required');
		if($posts['parent_email']){
			$this->form_validation->set_rules('parent_email', '电子邮箱', 'valid_email');
		}
		if($this->form_validation->run() == false){
			foreach($this->form_validation->error_array() as $filed => $error){
				$this->output_json([
					'retcode' => -1,
					'message' => $error
				]);
			}
		}
		$idno = strtoupper($posts['idno']);
		$signed = $this->signup_record_model->get_row([
			'select' => ['parent_name', 'parent_clan'],
			'where' => [
				'student_idno' => $idno
			]
		]);
		if($signed){
			$this->output_json('该学生已经由家长'.$signed['parent_name'].'报名登记');
		}
		
		// 性别 0=女、1=男
		$sex = substr($idno, 16, 1) % 2;
		$sex = ($sex !== 0) ? 1 : 0;
		
		// 生日
		$birthday = substr($idno, 6, 8);
		
		$data = [
			'user_id' => $this->uid,
			'school_id' => $posts['school_id'],
			'school' => $posts['school'],
			'grade_id' => $posts['grade_id'],
			'grade' => $posts['grade'],
			'campus_id' => $posts['campus_id'],
			'campus' => $posts['campus'],
			'student_name' => $posts['name'],
			'student_idno' => $idno,
			'student_sex' => $sex,
			'student_birthday' => date('Y-m-d', strtotime($birthday)),
			'nation' => $posts['nation'],
			'height' => $posts['height'],
			'weight' => $posts['weight'],
			'live_address' => $posts['live_address'],
			'family_address' => $posts['family_address'],
			'now_school' => $posts['now_school'],
			'now_grade' => $posts['now_grade'],
			'self_appraise' => $posts['appraise'],
			'honors' => $posts['honors'],
			'father_name' => $posts['father_name'],
			'father_phone' => $posts['father_phone'],
			'father_company' => $posts['father_company'],
			'mother_name' => $posts['mother_name'],
			'mother_phone' => $posts['mother_phone'],
			'mother_company' => $posts['mother_company'],
			'parent_name' => $posts['parent_name'],
			'parent_phone' => $posts['parent_phone'],
			'parent_clan' => $posts['parent_clan'],
			'parent_email' => strtolower($posts['parent_email']),
			'follow_way' => implode(',', $posts['follow_way']),
			'remark' => $posts['remark'],
			'inputtime' => time()
		];
		if(isset($posts['campus_adjustment'])){
			$data['campus_adjustment'] = $posts['campus_adjustment'];
		}
		if(isset($posts['study_way'])){
			$data['study_way'] = $posts['study_way'];
		}
		if($posts['school_id'] == 1 || $posts['school_id'] == 2){
		    $data['status'] = 1;
		}
		$query = $this->signup_record_model->set($data);
		if($query){
			$this->sendsms_notice($data['status']);
			$this->output_json('报名成功，系统审核中', 0);
		}
		$this->output_json('报名失败');
	}
	
	private function sendsms_notice($status){
		$posts = $this->input->post();
		$this->load->library('Aliyun/smslib');
		if($status == 1){
			$this->smslib->send_notice($posts['parent_phone'], [
				'parent' => $posts['parent_name'],
				'student' => $posts['name']
			], 'signup_passed');
		}else{
			$this->smslib->send_notice($posts['parent_phone'], [
				'parents_name' => $posts['parent_name'],
				'student_name' => $posts['name'],
				'campus' => $posts['school'].$posts['grade'].$posts['campus']
			], 'signup_success');
		}
	}
	
	public function delete(){
		$action = $this->action('delete');
		if($action){
			$this->output_json('删除成功', 0);
		}
		$this->output_json('删除失败');
	}
	
	private function action($method){
		$posts = $this->input->post();
		if(!$posts || !$posts['id']){
			$this->output_json();
		}
		switch($method){
			case 'delete':
				$query = $this->signup_record_model->del([
					'id' => $posts['id'],
					'user_id' => $this->uid
				]);
				break;
		}
		return $query;
	}
	
}